gusucode.com > 串口程序示例 MSCOMM控件 只要用来连通单片机与PC机 > 串口程序示例 MSCOMM控件 只要用来连通单片机与PC机/界面1/通信实验Dlg.cpp
// 通信实验Dlg.cpp : implementation file // #include "stdafx.h" #include "通信实验.h" #include "通信实验Dlg.h" #include <fstream> using namespace std; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif unsigned int CurItem = 0; unsigned int bianhao = 0; ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyDlg dialog CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(CMyDlg::IDD, pParent) { //{{AFX_DATA_INIT(CMyDlg) // m_strEditRXData = _T(""); m_strEditTXData = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMyDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDlg) DDX_Control(pDX, IDC_LIST2, m_list); DDX_Control(pDX, IDC_STOP, m_Stop); DDX_Control(pDX, IDC_PARITY, m_Parity); DDX_Control(pDX, IDC_DATABITS, m_DataBits); DDX_Control(pDX, IDC_COM, m_Com); DDX_Control(pDX, IDC_BAUD, m_Baud); // DDX_Text(pDX, IDC_EDIT_RXDATA, m_strEditRXData); DDX_Text(pDX, IDC_EDIT_TXDATA, m_strEditTXData); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMyDlg, CDialog) //{{AFX_MSG_MAP(CMyDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_MANUALSEND, OnButtonManualsend) ON_CBN_SELCHANGE(IDC_BAUD, OnSelchangeBaud) ON_CBN_EDITCHANGE(IDC_COM, OnEditchangeCom) ON_BN_CLICKED(IDC_BUTTON1, OnPortOpen) ON_BN_CLICKED(IDC_BUTTON2, OnPortClose) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyDlg message handlers BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here //几个组合框初始化 //波特率 CString str1[3]={"7200","9600","115200"}; int nIndex; for(int i=0;i<3;i++) { nIndex = m_Baud.AddString(str1[i]); m_Baud.SetItemData(nIndex,i); } m_Baud.SetCurSel(1); //串口 CString str2[]={"COM1","COM2","COM3","COM4","COM5","COM6"}; for(i=0;i<6;i++) { nIndex = m_Com.AddString(str2[i]); m_Com.SetItemData(nIndex,i); } m_Com.SetCurSel(0); //校验位 CString str3[]={"奇","偶","无"}; for(i=0;i<3;i++) { nIndex = m_Parity.AddString(str3[i]); m_Parity.SetItemData(nIndex,i); } m_Parity.SetCurSel(2); //数据位 CString str4[]={"8","9"}; for(i=0;i<2;i++) { nIndex = m_DataBits.AddString(str4[i]); m_DataBits.SetItemData(nIndex,i); } m_DataBits.SetCurSel(0); //校验位 CString str5[]={"1","2"}; for(i=0;i<2;i++) { nIndex = m_Stop.AddString(str5[i]); m_Stop.SetItemData(nIndex,i); } m_Stop.SetCurSel(0); //列表框控件初始化 m_list.InsertColumn(0,"编号",LVCFMT_LEFT,60,1); m_list.InsertColumn(1,"传感器类型",LVCFMT_LEFT,80,1); m_list.InsertColumn(2,"电阻比",LVCFMT_LEFT,100,1); m_list.InsertColumn(3,"电阻",LVCFMT_LEFT,100,1); m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); return TRUE; // return TRUE unless you set the focus to a control } void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CMyDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CMyDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } // BEGIN_EVENTSINK_MAP(CMyDlg, CDialog) // //{{AFX_EVENTSINK_MAP(CMyDlg) // //}}AFX_EVENTSINK_MAP // END_EVENTSINK_MAP() void CMyDlg::OnComm() { // TODO: Add your control notification handler code here BYTE rxdata[9];//设置BYTE数组 if(Damcom.Read(&rxdata,9) != 9){ MessageBox("数据接收错误"); return; } unsigned int *dianzubi = (unsigned int *)(&rxdata[1]); unsigned int *dianzu = (unsigned int*)(&rxdata[5]); int type; double dzb; double dz; type = rxdata[0]; dzb = (double)(*dianzubi)/1000; dz = (double)(*dianzu)/1000; char stype[5]; char sdianzubi[10]; char sdianzu[10]; char sbianhao[10]; bianhao++; itoa(type,stype,5);//整型转字符串 itoa(bianhao,sbianhao,10); gcvt((double)dzb,9,sdianzubi);//浮点数转字符串 gcvt((double)dz,9,sdianzu); m_list.InsertItem(CurItem,sbianhao); m_list.SetItemText(CurItem,1,stype); m_list.SetItemText(CurItem,2,sdianzubi); m_list.SetItemText(CurItem,3,sdianzu); // CurItem++; UpdateData(FALSE);//更新显示 ofstream tfile; tfile.open("data.txt",ios::app);//以追加方式打开data.txt文件 tfile<<type<<" "<<dzb<<" "<<dz<<" "<<endl; tfile.close(); } void CMyDlg::OnButtonManualsend() { // TODO: Add your control notification handler code here UpdateData(TRUE);//读取编辑框内容 //发送数据 Damcom.Write(m_strEditTXData); } void CMyDlg::OnButton2() { // TODO: Add your control notification handler code here } void CMyDlg::OnSelchangeBaud() { // TODO: Add your control notification handler code here int nIndexBaud = m_Baud.GetCurSel(); //获得当前选项的索引 } void CMyDlg::OnEditchangeCom() { // TODO: Add your control notification handler code here } void CMyDlg::OnPortOpen() { // TODO: Add your control notification handler code here DWORD comIndex = m_Com.GetCurSel()+1; // DWORD comIndex = m_Com.GetCurSel()+3; CString baud; m_Baud.GetLBText(m_Baud.GetCurSel(),baud); DWORD baudRate = atol(baud); if(!Damcom.Open(comIndex,baudRate)){//开始串口接收线程 CString prompt; prompt.Format("串口%d打开失败",comIndex); AfxMessageBox(prompt); return; } CString prompt; prompt.Format("串口%d打开成功",comIndex); AfxMessageBox(prompt); Damcom.SetWnd(this->GetSafeHwnd()); Damcom.SetBufferSize(1024,1024);//足够大的缓存 >> 31*15*8 Damcom.SetNotifyNum(9); } void CMyDlg::OnPortClose() { // TODO: Add your control notification handler code here Damcom.Close(); AfxMessageBox("串口已经关闭"); } BOOL CMyDlg::PreTranslateMessage(MSG* pMsg) { switch(pMsg->message){ case ON_COM_RECEIVE: OnComm(); } return CDialog::PreTranslateMessage(pMsg); }